<?php
class ControllerProductOption extends Controller {
	private $error = array();  
 
	public function index() {
		$this->load->language('product/option');
		$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('product/option');
		$this->getList();
	}

	public function insert() {
		$this->load->language('product/option');
		$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('product/option');
		if ($this->request->server['REQUEST_METHOD'] == 'POST') {
			$this->model_product_option->addOption($this->request->post);
			$this->session->data['success'] = $this->language->get('text_success');
			$this->redirect($this->url->link('product/option', 'token=' . $this->session->data['token'], 'SSL'));
		}
		$this->getFormText();
		$this->getInsertForm();
	}

	public function update() {
		$this->load->language('product/option');
		$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('product/option');
		if ($this->request->server['REQUEST_METHOD'] == 'POST') {
			$this->model_product_option->editOption($this->request->get['option_id'], $this->request->post);
			$this->session->data['success'] = $this->language->get('text_success');
			$this->redirect($this->url->link('product/option', 'token=' . $this->session->data['token'], 'SSL'));
		}
		$this->getFormText();
		$this->getUpdateForm();
	}

	private function getList() {
		$this->data['heading_title'] = $this->language->get('heading_title');
		$this->data['text_no_results'] = $this->language->get('text_no_results');
		$this->data['text_none'] = $this->language->get('text_none');
		$this->data['text_type_select'] = $this->language->get('text_type_select');
		$this->data['text_type_radio'] = $this->language->get('text_type_radio');
		$this->data['text_type_checkbox'] = $this->language->get('text_type_checkbox');
		$this->data['text_type_text'] = $this->language->get('text_type_text');
		$this->data['text_type_textarea'] = $this->language->get('text_type_textarea');
		$this->data['text_type_image'] = $this->language->get('text_type_image');
		$this->data['text_type_file'] = $this->language->get('text_type_file');
		$this->data['text_custom_no'] = $this->language->get('text_custom_no');
		$this->data['text_custom_yes'] = $this->language->get('text_custom_yes');
		$this->data['text_custom_number'] = $this->language->get('text_custom_number');
		$this->data['text_custom_words'] = $this->language->get('text_custom_words');
		
		$this->data['column_display_name'] = $this->language->get('column_display_name');
		$this->data['column_name'] = $this->language->get('column_name');
		$this->data['column_type'] = $this->language->get('column_type');
		$this->data['column_custom_type'] = $this->language->get('column_custom_type');
		$this->data['column_option_unit'] = $this->language->get('column_option_unit');
		$this->data['column_min_size'] = $this->language->get('column_min_size');
		$this->data['column_max_size'] = $this->language->get('column_max_size');
		$this->data['column_action'] = $this->language->get('column_action');	

		$this->data['button_insert'] = $this->language->get('button_insert');
		$this->data['button_filter'] = $this->language->get('button_filter');
 
		$this->data['token'] = $this->session->data['token'];
		
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}
		
  		$this->data['breadcrumbs'] = array();
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('product/option', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
		$this->data['insert'] = $this->url->link('product/option/insert', 'token=' . $this->session->data['token'], 'SSL');
		
		$url = '';
		if (isset($this->request->get['filter_display_name'])) {
			$filter_display_name = $this->request->get['filter_display_name'];
			$url .= '&filter_display_name=' . $this->request->get['filter_display_name'];
		}else{
			$filter_display_name = '';
		}
		if (isset($this->request->get['filter_type'])) {
			$filter_type = $this->request->get['filter_type'];
			$url .= '&filter_type=' . $this->request->get['filter_type'];
		}else{
			$filter_type = '-1';
		}
		$url2 = '';
		if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
			$url2 .= '&sort=' . $this->request->get['sort'];
		}else {
			$sort = 'od.display_name';
		}
		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
			$url2 .= '&order=' . $this->request->get['order'];
		}else {
			$order = 'ASC';
		}
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		}else {
			$page = 1;
		}
		 
		$this->data['options'] = array();
		$data = array(
			'filter_display_name'  => $filter_display_name,
			'filter_type'  => $filter_type,
			'sort'  => $sort,
			'order' => $order,
			'start' => ($page - 1) * $this->config->get('config_admin_limit'),
			'limit' => $this->config->get('config_admin_limit')
		);
		$option_total = $this->model_product_option->getTotalOptions($data);
		$results = $this->model_product_option->getOptions($data);
		foreach ($results as $result) {
			$action = array();
			$action[] = array(
				'text' => $this->language->get('text_edit'),
				'href' => $this->url->link('product/option/update', 'token=' . $this->session->data['token'] . '&option_id=' . $result['option_id'], 'SSL')
			);
			$this->data['options'][] = array(
				'option_id'   => $result['option_id'],
				'name'        => $result['name'],
				'display_name'=> $result['display_name'],
				'type'        => $this->showOptionType($result['type']),
				'option_unit' => $result['option_unit'],
				'min_size'    => $result['min_size'],
				'max_size'    => $result['max_size'],
				'action'      => $action
			);
		}
		
		$url_sort = $url;
		if ($order == 'ASC') {
			$url_sort .= '&order=DESC';
		} else {
			$url_sort .= '&order=ASC';
		}
		$this->data['sort_display_name'] = $this->url->link('product/option', 'token=' . $this->session->data['token'] . '&sort=od.display_name' . $url_sort, 'SSL');
		$this->data['sort_type'] = $this->url->link('product/option', 'token=' . $this->session->data['token'] . '&sort=o.type' . $url_sort, 'SSL');
		
		$url_page = $url . $url2;
		$pagination = new Pagination();
		$pagination->total = $option_total;
		$pagination->page = $page;
		$pagination->limit = $this->config->get('config_admin_limit');
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = $this->url->link('product/option', 'token=' . $this->session->data['token'] . $url_page . '&page={page}', 'SSL');
		$this->data['pagination'] = $pagination->render();
		
		$this->data['filter_display_name'] = $filter_display_name;
		$this->data['filter_type'] = $filter_type;
		$this->data['sort'] = $sort;
		$this->data['order'] = $order;

		$this->template = 'product/option_list.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
				
		$this->response->setOutput($this->render());
	}
	private function showOptionType($type) {
		//select;radio;checkbox;text;textarea;image;file
		switch ($type) {
			case "select":
		  		return $this->language->get('text_type_select');
			case "radio":
		  		return $this->language->get('text_type_radio');
			case "checkbox":
		  		return $this->language->get('text_type_checkbox');
			case "text":
		  		return $this->language->get('text_type_text');
			case "textarea":
		  		return $this->language->get('text_type_textarea');
			case "image":
		  		return $this->language->get('text_type_image');
			case "file":
		  		return $this->language->get('text_type_file');
			default:
		  		return $type;
		}
	}

	private function getFormText() {
		$this->data['heading_title'] = $this->language->get('heading_title');
		$this->data['text_none'] = $this->language->get('text_none');
		$this->data['text_choose'] = $this->language->get('text_choose');
		$this->data['text_type_select'] = $this->language->get('text_type_select');
		$this->data['text_type_radio'] = $this->language->get('text_type_radio');
		$this->data['text_type_checkbox'] = $this->language->get('text_type_checkbox');
		$this->data['text_type_text'] = $this->language->get('text_type_text');
		$this->data['text_type_textarea'] = $this->language->get('text_type_textarea');
		$this->data['text_type_image'] = $this->language->get('text_type_image');
		$this->data['text_type_file'] = $this->language->get('text_type_file');
		$this->data['text_custom_no'] = $this->language->get('text_custom_no');
		$this->data['text_custom_yes'] = $this->language->get('text_custom_yes');
		$this->data['text_custom_number'] = $this->language->get('text_custom_number');
		$this->data['text_custom_words'] = $this->language->get('text_custom_words');
		$this->data['text_image_manager'] = $this->language->get('text_image_manager');
		$this->data['text_browse'] = $this->language->get('text_browse');
		$this->data['text_clear'] = $this->language->get('text_clear');
		$this->data['text_option_value_remove_confirm'] = $this->language->get('text_option_value_remove_confirm');
		
		$this->data['column_display_name'] = $this->language->get('column_display_name');
		$this->data['column_name'] = $this->language->get('column_name');
		$this->data['column_type'] = $this->language->get('column_type');
		$this->data['column_custom_type'] = $this->language->get('column_custom_type');
		$this->data['column_option_unit'] = $this->language->get('column_option_unit');
		$this->data['column_min_size'] = $this->language->get('column_min_size');
		$this->data['column_max_size'] = $this->language->get('column_max_size');
		$this->data['column_action'] = $this->language->get('column_action');
		
		$this->data['entry_value_name'] = $this->language->get('entry_value_name');
		$this->data['entry_value_display_name'] = $this->language->get('entry_value_display_name');
		$this->data['entry_image'] = $this->language->get('entry_image');
		$this->data['entry_sort_order'] = $this->language->get('entry_sort_order');

		$this->data['button_save'] = $this->language->get('button_save');
		$this->data['button_cancel'] = $this->language->get('button_cancel');
		$this->data['button_add_option_value'] = $this->language->get('button_add_option_value');
		$this->data['button_remove'] = $this->language->get('button_remove');

		$this->data['token'] = $this->session->data['token'];
		$this->data['tab_general'] = $this->language->get('tab_general');
		
		$this->load->model('localisation/language');
		$this->data['languages'] = $this->model_localisation_language->getLanguages();
		
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
 		if (isset($this->error['name'])) {
			$this->data['error_name'] = $this->error['name'];
		} else {
			$this->data['error_name'] = array();
		}	
		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}

  		$this->data['breadcrumbs'] = array();
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('product/option', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
	}

	private function getInsertForm() {
		$this->data['action'] = $this->url->link('product/option/insert', 'token=' . $this->session->data['token'], 'SSL');
		$this->data['cancel'] = $this->url->link('product/option', 'token=' . $this->session->data['token'], 'SSL');
		
		$this->data['name'] = '';
		$this->data['type'] = 'select';
		$this->data['option_unit'] = '0';
		$this->data['min_size'] = '0';
		$this->data['max_size'] = '0';
		$this->data['option_description'] = array();
		$this->data['option_values'] = array();

		$this->load->model('tool/image');
		$this->data['no_image'] = $this->model_tool_image->resize('no_image.jpg', 100, 100);

		$this->template = 'product/option_form.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
				
		$this->response->setOutput($this->render());
	}

	private function getUpdateForm() {
		$this->data['action'] = $this->url->link('product/option/update', 'token=' . $this->session->data['token'] . '&option_id=' . $this->request->get['option_id'], 'SSL');
		$this->data['cancel'] = $this->url->link('product/option', 'token=' . $this->session->data['token'], 'SSL');

		$option_info = $this->model_product_option->getOption($this->request->get['option_id']);
		$this->data['type'] = $option_info['type'];
		$this->data['name'] = $option_info['name'];
		$this->data['option_unit'] = $option_info['option_unit'];
		$this->data['min_size'] = $option_info['min_size'];
		$this->data['max_size'] = $option_info['max_size'];
		$this->data['option_description'] = $this->model_product_option->getOptionDescriptions($this->request->get['option_id']);
		$option_values = $this->model_product_option->getOptionValueDescriptions($this->request->get['option_id']);
		$this->load->model('tool/image');
		$this->data['option_values'] = array();
		foreach ($option_values as $option_value) {
			if ($option_value['image'] && file_exists(DIR_IMAGE . $option_value['image'])) {
				$image = $option_value['image'];
			} else {
				$image = 'no_image.jpg';
			}
			$this->data['option_values'][] = array(
				'option_value_id'          => $option_value['option_value_id'],
				'name'                     => $option_value['name'],
				'image'                    => $image,
				'thumb'                    => $this->model_tool_image->resize($image, 100, 100),
				'sort_order'               => $option_value['sort_order'],
				'option_value_description' => $option_value['option_value_description']
			);
		}
		$this->data['no_image'] = $this->model_tool_image->resize('no_image.jpg', 100, 100);

		$this->template = 'product/option_form.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
				
		$this->response->setOutput($this->render());
	}
	
	public function autocomplete() {
		$json = array();
		if (isset($this->request->get['filter_display_name'])) {
			$this->load->model('product/option');
			$this->load->model('tool/image');
			$data = array(
				'filter_display_name' => $this->request->get['filter_display_name'],
				'start'       => 0,
				'limit'       => 20
			);
			$options = $this->model_product_option->getOptions($data);
			foreach ($options as $option) {
				$option_value_data = array();
				$option_values = $this->model_product_option->getOptionValues($option['option_id']);
				foreach ($option_values as $option_value) {
					if ($option_value['image'] && file_exists(DIR_IMAGE . $option_value['image'])) {
						$image = $this->model_tool_image->resize($option_value['image'], 50, 50);
					} else {
						$image = '';
					}
					$option_value_data[] = array(
						'option_value_id' => $option_value['option_value_id'],
						'name'            => html_entity_decode($option_value['name'], ENT_QUOTES, 'UTF-8'),
						'display_name'    => html_entity_decode($option_value['display_name'], ENT_QUOTES, 'UTF-8'),
						'image'           => $image,
						'sort_order'      => $option_value['sort_order']
					);
				}
				$json[] = array(
					'option_id'     => $option['option_id'],
					'type'          => html_entity_decode($option['type'], ENT_QUOTES, 'UTF-8'),
					'name'          => html_entity_decode($option['name'], ENT_QUOTES, 'UTF-8'),
					'display_name'  => html_entity_decode($option['display_name'], ENT_QUOTES, 'UTF-8'),
					'option_values' => $option_value_data
				);
			}
		}
		$this->response->setOutput(json_encode($json));
	}
}
?>